package sharetime.playlist.domain;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;

@Entity
@Table(name = "playlist_resource")
@NamedQueries({@NamedQuery(name = "getPlaylistResourcesByPlaylistId", query = "FROM PlaylistResource AS pr where pr.playlist.id = :playlistId")})
public class PlaylistResource extends Resource{

	private static final long serialVersionUID = -1061628611157639573L;
	
	private Integer ordinalRank;
    private Integer borrowedPlaylistId;
    
    private Playlist playlist;

    public Integer getOrdinalRank() {
        return ordinalRank;
    }

    public void setOrdinalRank(Integer ordinalRank) {
        this.ordinalRank = ordinalRank;
    }

    public Integer getBorrowedPlaylistId() {
        return borrowedPlaylistId;
    }

    public void setBorrowedPlaylistId(Integer borrowedPlaylistId) {
        this.borrowedPlaylistId = borrowedPlaylistId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_playlist_id")
    public Playlist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(Playlist playlist) {
        this.playlist = playlist;
    }

    @Transient
    public String getAbsoluteUrl() throws RESyntaxException {
        RE matcher = new RE("://");
        if (matcher.match(url))
            return url;
        else
            return "http://" + url;
    }

    @Transient
    public String getDisplayName() throws RESyntaxException {
        RE matcher = new RE("http://([^/]*)/");
        if (matcher.match(url)) {
            return matcher.getParen(1);
        } else {
            return "external link";
        }
    }
    

	@Override
	public String toString() {
		return this.description;
	}

}
